package dao

type User struct {
	GormModel
	Name     string `gorm:"type:varchar" json:"name" form:"name"`
	Account  string `gorm:"type:varchar;not null" json:"account" form:"account"`
	Password string `gorm:"type:varchar;not null" json:"-"`
	Roleid   int    `gorm:"type:varchar;not null" json:"roleid" form:"roleid"`
}

func (u *User) Insert() (err error) {
	err = db.Create(u).Error
	return
}

func (u *User) Save() (err error) {
	err = db.Model(u).Updates(User{Name: u.Name, Roleid: u.Roleid}).Error
	return
}

func (u *User) ChangePassword() (err error) {
	err = db.Model(u).Update("password", u.Password).Error
	return
}

func (u *User) FindById(id int) (us User) {
	db.First(&us, id)
	return
}
func (u *User) FindByAccount(account string) (us User) {
	db.Where("account=?", account).First(&us)
	return
}
func (u *User) FindByRoleid(roleid int) (us []User) {
	db.Where("roleid=?", roleid).Find(&us)
	return
}
func (u *User) Fetch() (us []User) {
	db.Find(&us)
	return
}

func (u *User) Delete(id int) {
	db.Where("id=?", id).Delete(u)
}
